//
//  NewsViewController.m
//  WXMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "NewsViewController.h"
#import "starView.h"
#import "NewsCell.h"
#import "NewsModel.h"
#import "ScrollView.h"
#import "NewsImageViewController.h"

@interface NewsViewController () <UIScrollViewDelegate>
{
//    UIImageView *_headImageView;
    ScrollView *_headImageView;
    
    NSMutableArray *_dataArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    starView *view = [[starView alloc] initWithFrame:CGRectMake(0, 0, 20, 100)];
//    [self.view addSubview:view];
    
    //数据解析
    [self _loadData];
    //界面创建
    [self _configUI];
    //数据加载
}

- (void)_loadData {
    
    _dataArray = [NSMutableArray array];
    
    NSArray *array = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"news_list.json" ofType:nil]] options:NSJSONReadingMutableContainers error:nil];
    
    
    for (NSDictionary *dic in array) {
        NewsModel *model = [[NewsModel alloc] init];
        
        model.title = dic[@"title"];
        model.summary = dic[@"summary"];
        model.image = dic[@"image"];
        model.newsId = [dic[@"newid"] integerValue];
        model.newsType = (NewsType)[dic[@"type"] integerValue];
        
        [_dataArray addObject:model];
    }
}



- (void)_configUI {
    
    self.tableView.separatorColor = [UIColor colorWithWhite:0.32 alpha:1];
    self.tableView.rowHeight = 100;
//加载滑动视图
    _headImageView = [[ScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 150)];

    [self.view insertSubview:_headImageView aboveSubview:self.tableView];
    self.tableView.contentInset = UIEdgeInsetsMake(150 , 0, 0, 0);

    
    [self.tableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:@"NewsCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell" forIndexPath:indexPath];
    cell.model = _dataArray[indexPath.row];
    

    
    return cell;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count ;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    _headImageView.scroll.scrollEnabled = NO;
    CGFloat y = scrollView.contentOffset.y + scrollView.contentInset.top;

    if (y <=0) {
        CGFloat width = kScreenWidth * (150 - y) / 150;
        CGFloat height = 150 - y;
        
        _headImageView.scroll.frame = CGRectMake((kScreenWidth - width)/2, 0, width, height);
        
        _headImageView.imageView1.frame = CGRectMake(0, 0, width, height);
        _headImageView.imageView2.frame = CGRectMake(kScreenWidth, 0, width, height);
        _headImageView.imageView3.frame = CGRectMake(2 * width, 0, width, height);
        _headImageView.titleLabel2.frame = CGRectMake(0, height - 20, width, 20);

    }else if (y != 0){
        
        _headImageView.frame = CGRectMake(0, -y , kScreenWidth, 150);
        

    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *viewCtrlID = nil;
    switch (cell.model.newsType) {
        case WordType:
            
            viewCtrlID = @"NewsWordViewController";
            break;
        
        case ImageType:
            viewCtrlID = @"NewsImageViewController";
            break;
        
        case VideoType:
            viewCtrlID = @"NewsWordViewController";
            break;
        default:
            break;
    }
    
    UIViewController *viewCtrl = [self.storyboard instantiateViewControllerWithIdentifier:viewCtrlID];
    viewCtrl.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewCtrl animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
